package com.guli.gulimall.gulimallproduct.service.impl;

import com.guli.common.utils.R;
import com.guli.gulimall.gulimallproduct.service.CategoryBrandRelationService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.gulimallproduct.dao.CategoryDao;
import com.guli.gulimall.gulimallproduct.entity.CategoryEntity;
import com.guli.gulimall.gulimallproduct.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
@Transactional //表示这是一个是事务
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        List<CategoryEntity> entitiess = entities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0 && categoryEntity.getShowStatus() != 0).map((menu) -> {
            menu.setChildren(getChilden(menu, entities));
            return menu;
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
        return entitiess;
    }

    @Override
    public R removeMenuByIds(List<Long> asList) {
        if (null != asList && asList.size() > 0) {
            int num = baseMapper.deleteBatchIds(asList);
            return R.ok().put("category", num);
        }
        return R.error().put("category", 0);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        findPareentPath(catelogId, paths);
        return paths.toArray(new Long[paths.size()]);
    }

    /**
     * 级联更新所有关联的数据
     *
     * @param category
     * @return
     */

    @Override
    public boolean updateCascade(CategoryEntity category) {
        boolean check = true;
        if (!category.getName().isEmpty()) {
            /**
             * 更新关联的数据
             */
            check = categoryBrandRelationService.updateCategpry(category.getCatId(), category.getName());
        }
        return check ? check&&this.updateById(category) : false;
    }

    private void findPareentPath(Long catelogId, List<Long> paths) {
        CategoryEntity categoryEntity = this.baseMapper.selectById(catelogId);
        if (categoryEntity == null) return;
        paths.add(categoryEntity.getCatId());
        if (categoryEntity.getParentCid() != 0)
            findPareentPath(categoryEntity.getParentCid(), paths);

    }


    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getChilden(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> categoryEntity.getParentCid() == root.getCatId() && categoryEntity.getShowStatus() != 0)
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getChilden(categoryEntity, all));
                    return categoryEntity;
                }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
        return children;
    }

}